﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.Banner;
using JLL.China.Infrastructure.RepositoryFramework;
using JLL.China.Infrastructure.Repositories.LINQ2SQL;

namespace JLL.China.Infrastructure.Services
{
    public static class BannerService
    {
        private static BannerRepository repository;

        static BannerService()
        {
            repository = RepositoryFactory.GetRepository<BannerRepository, Model.Banner, int>(new LINQ2SQLUnitOfWork<int>());
        }

        public static IList<Model.Banner> FindByGroupFid(decimal id)
        {
            return repository.FindByGroupFid(id);

        }

        public static IList<Model.Banner> GetAllBanner(string type)
        {
            return repository.FindAllBanner(type);
        }

        /// <summary>
        /// get all static and dynamic side contents
        /// </summary>
        /// <returns></returns>
        public static IList<Model.Banner> GetDistinctSideContent()
         {
             using (var db = new JLLCHINADataContext())
             {
                 var ls = (from r in db.Banners
                          where r.BannerGroupFID.Equals(3) //side banner
                          //where r.Type.Equals("dynamic")  
                          where r.LanguageFID.Equals(1) 
                          where r.ParentID.Equals(r.Banner_ID)  //group banner
                          select r).ToList();
                 return ls.Select(banner => BannerFactory.FromLINQObject(banner)).ToList();
             }
         }
       

       /// <summary>
        /// get main dynamic/static side content which is parentid = banner_id
       /// </summary>
       /// <param name="id">1 dynamic, 2 static</param>
       /// <param name="parentid"></param>
       /// <returns></returns>
        public static Model.Banner GetMainSideContent(int id,decimal? parentid)
        {
            if (parentid == null) return null;
            using (var db = new JLLCHINADataContext())
            {
                if (id == 1)
                {
                    var banner = (from r in db.Banners
                                  where r.BannerGroupFID.Equals(3) //side banner
                                  where r.Type.Equals("dynamic")
                                  where r.LanguageFID.Equals(1)
                                  where r.ParentID.Equals(r.Banner_ID)  //group banner
                                  where r.ParentID.Equals(parentid)
                                  select r).SingleOrDefault();
                    return BannerFactory.FromLINQObject(banner);
                }
                else
                {
                    var banner = (from r in db.Banners
                                  where r.BannerGroupFID.Equals(3) //side banner
                                  where r.Type.Equals("static")
                                  where r.LanguageFID.Equals(1)
                                  where r.ParentID.Equals(r.Banner_ID)  //group banner
                                  where r.ParentID.Equals(parentid)
                                  select r).SingleOrDefault();
                    return BannerFactory.FromLINQObject(banner);
                }
               
            }
        }
       
        

        /// <summary>
        /// get all dynamic/static content in group side content
        /// </summary>
        /// <param name="id">1 dynamic, 2 static</param>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public static IList<Model.Banner> GetAllSideContentInGroup(int id,decimal? parentID)
        {
            if (parentID == null) return null;
            using (var db = new JLLCHINADataContext())
            {
                if (id==1)
                {
                    var ls = (from r in db.Banners
                              where r.BannerGroupFID.Equals(3) //side banner
                              where r.Type.Equals("dynamic")
                              where r.LanguageFID.Equals(1)
                              where r.ParentID.Equals(parentID)
                              where r.ParentID != r.Banner_ID//group banner
                              select r).ToList();
                    return ls.Select(banner => BannerFactory.FromLINQObject(banner)).ToList();
                }
                else
                {
                    var ls = (from r in db.Banners
                              where r.BannerGroupFID.Equals(3) //side banner
                              where r.Type.Equals("static")
                              where r.LanguageFID.Equals(1)
                              where r.ParentID.Equals(parentID)
                              where r.ParentID != r.Banner_ID//group banner
                              select r).ToList();
                    return ls.Select(banner => BannerFactory.FromLINQObject(banner)).ToList();
                }
               
            }
        }

        /// <summary>
        /// get all side content
        /// </summary>
        /// <param name="id">1 is dynamic, 2 is static</param>
        /// <returns></returns>
        public static IList<Model.Banner> GetAllMainSideContentsInGroup(int id)
        {
            using (var db = new JLLCHINADataContext())
            {
                if (id == 1)
                {
                    var ls = (from r in db.Banners
                              where r.BannerGroupFID.Equals(3) //side banner
                              where r.Type.Equals("dynamic")
                              where r.LanguageFID.Equals(1)
                              where r.ParentID.Equals(r.Banner_ID)  //group banner
                              select r).ToList();
                    return ls.Select(banner => BannerFactory.FromLINQObject(banner)).ToList();
                }
                else
                {
                    var ls = (from r in db.Banners
                              where r.BannerGroupFID.Equals(3) //side banner
                              where r.Type.Equals("static")
                              where r.LanguageFID.Equals(1)
                              where r.ParentID.Equals(r.Banner_ID)  //group banner
                              select r).ToList();
                    return ls.Select(banner => BannerFactory.FromLINQObject(banner)).ToList();
                }
            }
        }
 

        public static decimal UpdateParentBanner(Model.Banner model)
        {
            return repository.UpdateBannerParent(model);
        }

        public static IList<Model.Banner> FindByGroupFidAndLanguage(decimal? id, byte language)
        {
            return repository.FindByGroupFidAndLanguage(id, language);
        }
        public static Model.Banner FindByID_LanguageFid(decimal? id, byte languageFid)
        {
            return repository.FindByID_LanguageFid(id, languageFid);
        }
        public static decimal CreateBanner(Model.Banner banner)
        {
            
            return repository.CreateBanner(banner);
           
        }

        public static decimal UpdateBanner(Model.Banner banner)
        {
            
            return repository.UpdateBanner(banner);
            

        }

        public static decimal CreateBanner(Model.Banner banner, Model.Banner bannerLocal)
        {
            bannerLocal.BannerUrl = banner.BannerUrl;
            bannerLocal.Link = banner.Link;
            bannerLocal.BannerGroupFID = banner.BannerGroupFID;
            bannerLocal.Sequence = banner.Sequence;
            bannerLocal.Status = banner.Status;
            bannerLocal.Type = banner.Type;
            banner.LanguageFID = 1;
            bannerLocal.LanguageFID = 2;
            decimal id = repository.CreateBanner(banner);
            bannerLocal.Banner_ID = id;
            return repository.CreateBanner(bannerLocal);
        }
        public static decimal UpdateBanner(Model.Banner banner, Model.Banner bannerLocal)
        {
            bannerLocal.BannerUrl = banner.BannerUrl;
            bannerLocal.Link = banner.Link;
            bannerLocal.BannerGroupFID = banner.BannerGroupFID;
            bannerLocal.Sequence = banner.Sequence;
            bannerLocal.Status = banner.Status;
            bannerLocal.Type = banner.Type;
            bannerLocal.Banner_ID = banner.Banner_ID;
            banner.LanguageFID = 1;
            bannerLocal.LanguageFID = 2;
            decimal id = repository.UpdateBanner(banner);
            return repository.UpdateBanner(bannerLocal);

        }
        public static decimal DeleteBanner(decimal? bannerId)
        {
            return repository.DeleteBanner(bannerId);
        }

        /**
         * delete all static/dynamic banner
         */
        public static decimal DeleteStaticDynamic(decimal? parentId)
        {
            using (var db = new JLLCHINADataContext())
            {
                try
                {
                    var ls = (from r in db.Banners
                              where r.BannerGroupFID.Equals(3) //side banner
                              where r.ParentID.Equals(parentId)  //group banner
                              select r).ToList();

                    db.Banners.DeleteAllOnSubmit(ls);
                    db.SubmitChanges();
                    return 1;
                }
                catch (Exception)
                {

                    return -1;
                }
                    
                
            }
        }
        public static decimal ChangeStatusDynamicStaticByParentIds(string parentIds)
        {
            using (var db = new JLLCHINADataContext())
            {
                try
                {
                    string[] pIds = parentIds.Split(',');
                    foreach (var pId in pIds)
                    {
                        
                        var ls = (from r in db.Banners
                                  where r.BannerGroupFID.Equals(3) //side banner
                                  where r.ParentID.Equals(decimal.Parse(pId))  //group banner
                                  select r).ToList();
                        foreach (var banner in ls)
                        {
                            try
                            {
                                banner.Status = !banner.Status;
                                // db.Banners.InsertOnSubmit(banner);
                                db.SubmitChanges();
                            }
                            catch (Exception)
                            {
                                
                               continue;
                            }
                            
                        }
                       
                    }
                   
                    return 1;
                }
                catch (Exception)
                {

                    return -1;
                }


            }
        }
        public static decimal ChangeStatusDynamicStaticByBannerIds(string bannerIds)
        {
            using (var db = new JLLCHINADataContext())
            {
                try
                {
                    string[] bIds = bannerIds.Split(',');
                    foreach (var bId in bIds)
                    {
                        var ls = (from r in db.Banners
                                 // where r.BannerGroupFID.Equals(3) //side banner
                                  where r.Banner_ID.Equals(decimal.Parse(bId))  //group banner
                                  select r).ToList();
                        foreach (var banner in ls)
                        {
                            banner.Status = banner.Status!=null ? !banner.Status:false;
                            //db.Banners.InsertOnSubmit(banner);
                            db.SubmitChanges();
                        }

                    }

                    return 1;
                }
                catch (Exception)
                {

                    return -1;
                }


            }
        }

        public static void ChangeStatus(string arrIds, bool status)
        {
            repository.ChangeStatus(arrIds, status);
        }

        public static void RemoveSelected(string arrIds)
        {
            repository.RemoveSelected(arrIds);
        }
        /// <summary>
        /// update sequence for banner
        /// </summary>
        /// <param name="bannerid"></param>
        /// <param name="pos"></param>
        public static void UpdateOrder(decimal bannerid, int pos)
        {
            try
            {
                using (var dbc = new JLLCHINADataContext())
                {
                    var lb = from c in dbc.Banners
                             where c.Banner_ID.Equals(bannerid)
                             select c;

                    foreach (var b in lb)
                    {

                        b.Sequence = pos;

                        dbc.SubmitChanges();

                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }


        public static void ChangeGroupStatus(string arrayParentId, bool? status)
        {
            status = (status == null) ? false : status;
            repository.ChangeGroupStatus(arrayParentId, status);
        }

        public static void RemoveSelectedGroup(string arrayId)
        {
            repository.RemoveSelectedGroup(arrayId);
        }

    }
}
